<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/8/29 0029
 * Time: 下午 5:34
 */
namespace app\wx\controller;
use think\Controller;
use think\Db;
use think\Request;
use think\Validate;
use app\wx\controller\Home;

class Index extends Home
{
    //protected $needAuth = true; //微信授权登陆
    //----------------------------------------------页面主功能-------------------------------------------------------------------

    //首页
    public function index(){
        //查询个人信息
        $this->view->regiserInfo = Db::name('core_register')->where('id',session('user_id'))->find();
        return $this->fetch();
    }

    //修改密码
    public function setPassword(){
        $this->is_login();
        if($this->request->isPost()){
            $data = input('post.');
            if($data['password'] != $data['password_show']){
                return json(['msg'=>'两次密码不一致，请重新填写']);
            }
            $password = Db::name('core_register')->where('id',session('user_id'))->value('password');
            if($password != md5($data['last_password'])){
                return json(['msg'=>'原密码填写错误，请从新填写']);
            }
            $data['password'] = md5($data['password']);
            unset($data['last_password']);
            Db::name('core_register')->where('id',session('user_id'))->update($data);
            return json(200);
        }
    }

    //更改账户
    public function setPhone(){
        $this->is_login();
        if($this->request->isPost()){
            $data = input('post.');
//            if($data['password'] != $data['password_show']){
//                return json(['msg'=>'两次密码不一致，请重新填写']);
//            }
            $validate = new Validate([
                ['password','require','原密码不能为空'],
                ['phone','require','手机号不能为空'],
                ['code','require','验证码不能为空'],
            ]);
            if(!$validate->check($data)) return json(array('msg'=>$validate->getError()));
            if($data['code'] != session('code')) return json(['msg'=>'验证码错误']);
            session('code',null);
            unset($data['code']);
            unset($data['phone']);
            $password = Db::name('core_register')->where('id',session('user_id'))->value('password');
            if($password != md5($data['password'])){
                return json(['msg'=>'原密码填写错误，请从新填写']);
            }
            Db::name('core_register')->where('id',session('user_id'))->update($data);
            return json(200);
        }
    }
}